package br.com.evoluy.repositorio;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import br.com.evoluy.entidade.LogAcesso;
import br.com.evoluy.util.ConnectionFactory;

public class RepositorioLogAcesso {
		
	//Hibernate
	public void saveLogAcesso(LogAcesso log)
	{
		EntityManagerFactory factory = Persistence.createEntityManagerFactory("sis");
		EntityManager em = factory.createEntityManager();
			
	    em.getTransaction().begin();
	    em.persist(log);
	    em.getTransaction().commit();
	       
	    factory.close();
		em.close();			
	}
	
	//JDBC
	public void saveLogAcessoJDBC(LogAcesso log)
	{
        Connection con = new ConnectionFactory().getConnection();
		String sql = "insert into logacesso"+"(id,idusuario,nomeusuario,sobrenome,senha,psf,timestamp,tipoacesso)"+
                     "values(?,?,?,?,?,?,?,?)";
		
		try 
		{	
			PreparedStatement stmp = (PreparedStatement) con.prepareStatement(sql);
			stmp.setLong(1, log.getId());
			stmp.setLong(2, log.getIdusuario());
			stmp.setString(3, log.getNomeusuario());
			stmp.setString(4, log.getSobrenome());
			stmp.setString(5, log.getSenha());
			stmp.setString(6, log.getPsf());
			stmp.setString(7, log.getTimestamp());
			stmp.setString(8, log.getTipoacesso());
			stmp.execute();
			stmp.close();
		} 
		catch (SQLException e) 
		{	
			e.printStackTrace();
		}
		finally
		{
			try 
			{
				con.close();
			} 
			catch (SQLException e) 
			{
				e.printStackTrace();
			}
		}
	}

}
